﻿using System.IO;
using System.Web;
using System.Web.UI;
using System.Configuration;
using System.Text;

namespace Channelvn.Cached.Common
{
    public class Utilities
    {
        public static Page LoadPage(string url, HttpContext context)
        {

            return (Page)PageParser.GetCompiledPageInstance(url, context.Server.MapPath(url), context);
        }


        public static string RenderPage(Page page, HttpContext context)
        {
            StringWriter writer = new StringWriter();

            context.Server.Execute(page, writer, true);
            string htmlCode = writer.ToString().Trim();
            if (ConfigurationManager.AppSettings["AllowWrite1Line"] == "1")
            {
                htmlCode = htmlCode.Replace("\t", string.Empty);
                htmlCode = htmlCode.Replace("    ", string.Empty);
                htmlCode = htmlCode.Replace("  ", " ");
            }
            return htmlCode;
        }
        public static int GetCurrVersion(string keyVersion)
        {
            int version = 1;
            if (!string.IsNullOrEmpty(keyVersion))
            {
                int startIndex = keyVersion.IndexOf("_Version:");
                if (startIndex > 0)
                {
                    startIndex += 9;
                    string temp = keyVersion.Substring(startIndex, keyVersion.Length - startIndex - 1);
                    int.TryParse(temp, out version);
                }
            }
            return version;
        }
    }
}
